﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using DbSharp.Framework.Driver;

namespace DbSharp.Framework.DbObject
{
    public class DbCmdScalarExecution
    {

        public DbCmdScalarResult ExecuteScalar(DbCommand cmd, ITestDataSource dbPlugin)
        {
            object value = null;
            int valueType = -1;

            var reader = cmd.ExecuteReader(CommandBehavior.SingleRow | CommandBehavior.SingleResult);
            if (reader == null)
            {
                value = null;
                valueType = -1;
            }
            try
            {
                if (reader.Read())
                {
                    value = reader.GetValue(0);
                    valueType = dbPlugin.FieldPersistenceService.GetSpecificDbType(reader, 0);
                }
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                reader = null;
            }

            DbCmdScalarResult result = new DbCmdScalarResult(value, valueType);
            return result;
        }






    }
}
